<template>
  <rt-list
    ref="moresearchList"
    :table-name="tableName"
    :columns="columns"
    :operations="operations"
    :custom-operations="customOperations"
    :custom-button-operation="customButtonOperations"
    :title="title"
    :title-list="menuList"
    :hide-title="true"
    :input-search-param="quickSearch"
    :controller-name="controllerName"
    :customize-action="customizeAction"
    search-placeholder="根据活动名称进行搜索"
  />
</template>

<script>
import RtList from '../../components/custome/rtList.vue';
export default {
  name: 'CampaignList',
  components: {
    RtList
  },
  data() {
    return {
      menuList: {
        key: 'bbs_campaign_status',
        selected: 0,
        value: [
          {
            name: '已发布活动',
            value: '2'
          },
          {
            name: '未发布活动',
            value: '1'
          },
          {
            name: '所有的活动',
            value: '-1'
          }
        ]
      },
      controllerName: 'bbs/campaign',
      tableName: 'bbs_campaign',
      title: '自定义查询列表',
      operations: ['releasebatch', 'dereleasebatch', 'delete', 'search', 'new'],
      queryValue: '',
      columns: [
        {
          prop: 'name',
          label: '活动名称',
          sortable: true,
          type: 'customizeAction',
          actionId: 'bbs_campaignid'
        },
        {
          prop: 'code',
          label: '活动编号',
          sortable: true,
          autoCol: true
        },
        {
          prop: 'start_time',
          sortable: true,
          label: '开始时间',
          format: 'YYYY-MM-DD HH:mm'
        },
        {
          prop: 'end_time',
          sortable: true,
          label: '结束时间',
          format: 'YYYY-MM-DD HH:mm'
        },
        {
          prop: 'is_display_start_page',
          label: '在启动页展示',
          sortable: true,
          map: row => `${row.is_display_start_page ? '是' : '否'}`
        },
        {
          prop: 'bbs_campaign_statusname',
          label: '状态',
          sortable: true,
          autoCol: true
        }
      ],
      tableParam: {
        name: ''
      },
      quickSearch: ['name'],
      customOperations: [
        {
          operate: 'releasebatch',
          operateName: '发布',
          iconName: 'rt-portal-able'
        },
        {
          operate: 'dereleasebatch',
          operateName: '取消发布',
          iconName: 'rt-portal-disabled'
        }
      ],
      customButtonOperations: {
        releasebatch: ids => {
          if (ids.length === 0) {
            rt.showErrorToast('请至少选择一条记录');
          } else {
            this.$confirm(
              `您一共选择了${ids.length}条记录，确认要发布吗？`,
              '提示',
              {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }
            )
              .then(() => {
                rt.post(`api/bbs/campaign/releasebatch`, ids)
                  .then(res => {
                    rt.showSuccessToast('发布成功');
                    this.$refs.moresearchList.refreshTable();
                  })
                  .catch(error => {
                    rt.showErrorToast(error);
                  });
              })
              .catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消发布'
                });
              });
          }
        },
        dereleasebatch: ids => {
          if (ids.length === 0) {
            rt.showErrorToast('请至少选择一条记录');
          } else {
            this.$confirm(
              `您一共选择了${ids.length}条记录，确认要取消发布吗？`,
              '提示',
              {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }
            )
              .then(() => {
                rt.post(`api/bbs/campaign/dereleasebatch`, ids)
                  .then(res => {
                    rt.showSuccessToast('取消发布成功');
                    this.$refs.moresearchList.refreshTable();
                  })
                  .catch(error => {
                    rt.showErrorToast(error);
                  });
              })
              .catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消'
                });
              });
          }
        }
      }
    };
  },
  created() {},
  mounted() {},
  methods: {
    customizeAction(row, actionId) {
      var param = {
        id: row[actionId]
      };
      this.$router.push({
        name: 'campaign-detail',
        params: param
      });
    }
  }
};
</script>
